package upcweb.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import upcweb.modelo.ClsAcceso;
import upcweb.modelo.ClsOferta;
import upcweb.modelo.ClsUsuario;
import upcweb.negocio.BsAccesos;
import upcweb.negocio.BsGestionOfertas;
import upcweb.util.CheckLogin;

@Controller
public class IndexController {

	@RequestMapping(value = "/index", method = RequestMethod.GET)
	protected ModelAndView index(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("index");
		return this.getMVListOferta(mv);
	}

	@RequestMapping(value = "/goPage", method = RequestMethod.POST)
	protected ModelAndView goPage(HttpServletRequest request,
			HttpServletResponse response) {
		// response.
		//System.out.println("Pagina : " + request.getParameter("page"));
		CheckLogin chk = new CheckLogin();
		ModelAndView mv = new ModelAndView("mvGo");
		mv.addObject("acceso","");
		String page = request.getParameter("page");
		ClsUsuario logUser = chk.getSession(request, response);
		if (logUser!=null){
			System.out.println("Loged");
			if (page.equalsIgnoreCase("inicio.html")
				|| page.equalsIgnoreCase("endSesion.html")) {	
				mv.addObject("acceso","X");
				return mv;
			}
			else {
				ClsAcceso ac= new ClsAcceso();
				ac.setNo_user(logUser.getNo_usuario());
				ac.setUrl_Objeto(page);
				if(new BsAccesos().checkAcceso(ac)){
					mv.addObject("acceso","X");
				}
				return mv;
			}
		} else {
			if (page.equalsIgnoreCase("inicio.html")
					|| page.equalsIgnoreCase("endSesion.html")) {	
					mv.addObject("acceso","X");
					return mv;
				}
			System.out.println("Not Loged");
			ModelAndView mv2 = new ModelAndView("mvLogin");
			mv2.addObject("no", "X");
			// mv.addObject("end","false");
			return mv2;
		}
	}

	@RequestMapping(value = "/goOper", method = RequestMethod.POST)
	protected ModelAndView goOperacion(HttpServletRequest request,
			HttpServletResponse response) {
		// response.
		// System.out.println("Pagina : " + request.getParameter("page"));
		CheckLogin chk = new CheckLogin();
		if (chk.isSession(request, response)
				|| request.getParameter("page").equalsIgnoreCase("inicio.html")
				|| request.getParameter("page").equalsIgnoreCase(
						"endSesion.html")) {
			ModelAndView mv = new ModelAndView("mvGo");
			mv.addObject("acceso","X");
			return mv;
		} else {
			ModelAndView mv = new ModelAndView("mvLogin");
			mv.addObject("no", "X");
			// mv.addObject("end","false");
			return mv;
		}
	}

//	@RequestMapping(value = "/club", method = RequestMethod.POST)
//	protected ModelAndView goClubs(HttpServletRequest request,
//			HttpServletResponse response) {
//		return new ModelAndView("clubs/mvClub");
//	}

	private ModelAndView getMVListOferta(ModelAndView mv) {
		ArrayList<ClsOferta> lstOferta = new ArrayList<ClsOferta>();
		BsGestionOfertas oBsGestionOfertas = new BsGestionOfertas();
		lstOferta = oBsGestionOfertas.getOfertasNoVencidas();
		
		int icontIndex1=0;
		int icontIndex2=0;
		int icontIndex3=0;
		
		for(ClsOferta oClsOferta : lstOferta){
			if(oClsOferta.getNo_ubic_page().equals("1")){
				icontIndex1++;
			}
			if(oClsOferta.getNo_ubic_page().equals("2")){
				icontIndex2++;
			}
			if(oClsOferta.getNo_ubic_page().equals("3")){
				icontIndex3++;
			}			
		}
		mv.addObject("index1", icontIndex1);
		mv.addObject("index2", icontIndex2);
		mv.addObject("index3", icontIndex3);
		mv.addObject("lstOferta", lstOferta);
		return mv;
	}

}
